package com.example.tourstor;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class WithDrawActivity extends AppCompatActivity {
    private static final String TAG ="WithDrawActivity";
    private TextView title,hint;
    private EditText drawmoney;
    private Button title_btn_left,title_btn_right,withDraw;
    private Thread t;
    private Connection con ;
    private PreparedStatement ps=null,ps1;//操作整合sql语句的对象
    private ResultSet rs=null;
    DBOpenHelper dbOpenHelper;
    private String tstart,smoney,withmoney,name;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_with_draw);
        setTitle();

        final SharedPreferences sharedPreferences=getSharedPreferences("Cname", Activity.MODE_PRIVATE);
        //name=sharedPreferences.getString("Cname",null);
        name="41623060";
        Log.i(TAG, "MyPlanActivity2: 登录名是："+name);


        Intent intent=getIntent();
        tstart=intent.getStringExtra("Tstart");
        smoney=intent.getStringExtra("Smoney");
        Log.i(TAG, "onCreate: 传入的值是："+tstart+smoney);


        drawmoney=findViewById(R.id.drawmoney);
        withDraw=findViewById(R.id.withdraw1);
        hint=findViewById(R.id.hint);

        hint.setText("当前余额为"+smoney+"元");

        final float smoney1= Float.parseFloat(smoney);
        final Date date1= Date.valueOf(tstart);

        withDraw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                withmoney=drawmoney.getText().toString();
                Log.i(TAG, "onClick: 提现金额是："+withmoney);
                if(withmoney!=null){
                    final float withmoney1= Float.parseFloat("".equals(withmoney.toString())?"0.00":withmoney.toString());
                    t=new Thread(new Runnable() {

                        @Override
                        public void run() {
                            con=dbOpenHelper.getConn();
                            //String sql2="select * from makeplan where Uid= '"+name+"' and  datediff ( Tstart , '"+date+"' ) = 0 ";
                            String sql = "update makeplan set Smoney= "+(smoney1-withmoney1)+" where datediff ( Tstart , '"+date1+"' ) = 0";
                            String sql1 = "insert into withdraw(Uid,Withdmoney) values(?,?)";
                            try {
                                ps = con.prepareStatement(sql);
                                ps.executeUpdate();
                                Log.i(TAG, "init: 更新成功");

                                ps1=con.prepareStatement(sql1);
                                ps1.setString(1,name);
                                ps1.setFloat(2,withmoney1);
                                ps1.executeUpdate();

                                con.close();//一定要关闭
                                ps.close();
                                ps1.close();
                            }
                            catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    t.start();

                }else {

                    Toast.makeText(WithDrawActivity.this,"请输入提现金额",Toast.LENGTH_LONG).show();
                }

            }
        });


    }
    public void setTitle(){
        title = findViewById(R.id.title_text);
        title.setText("我要提现");
        title_btn_left = findViewById(R.id.title_btn_left);
        title_btn_right=findViewById(R.id.title_btn_right);
        title_btn_left.setText("");
        title_btn_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        title_btn_right.setText("");
        title_btn_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
